package cn.getech.data.development.utils;

import cn.getech.data.intelligence.common.exception.RRException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {

    public static final String DATE = "yyyy-MM-dd";
    public static final String DATETIME = "yyyy-MM-dd HH:mm:ss";

    public static String changeStartDateTime(LocalDateTime time) {
        return DateTimeFormatter.ofPattern(DATE).format(time) + " 00:00:00";
    }
    public static String changeEndDateTime(LocalDateTime time) {
        return DateTimeFormatter.ofPattern(DATE).format(time) + " 23:59:59";
    }
    public static String changeDate(LocalDateTime time) {
        return DateTimeFormatter.ofPattern(DATE).format(time);
    }
    public static String changeDate(Date time) {
        return new SimpleDateFormat(DATE).format(time);
    }
    public static String changeDateTime(Date time) {
        return new SimpleDateFormat(DATETIME).format(time);
    }

    public static String getCurrentTime(LocalDateTime now){
        String format = DateTimeFormatter.ofPattern("yyyy:MM:dd:HH:mm:ss").format(now);
        return format.replace(":","");
    }

    public static String changeLocalDateTime(LocalDateTime time){
        String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(time);
        return format;
    }

    public static LocalDateTime cheackDateTime(String time){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(time,df);
    }

    /**
     * 获取本周的第一天
     *
     * @return String
     **/
    public static String getWeekStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }

    /**
     * 获取本周的最后一天
     *
     * @return String
     **/
    public static String getWeekEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
    }

    /**
     * 获取本月开始日期
     *
     * @return String
     **/
    public static String getMonthStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }

    /**
     * 获取本月最后一天
     *
     * @return String
     **/
    public static String getMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
    }

    /**
     * 获取两个时间段的所有日期
     *
     * @return List<String>
     **/
    public static List<String> getTimeOriginalList(String startDate, String endDate) {
        SimpleDateFormat sdf;
        int calendarType;

        switch (startDate.length()) {
            case 19:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                calendarType = Calendar.DATE;
                break;
            case 10:
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                calendarType = Calendar.DATE;
                break;
            case 7:
                sdf = new SimpleDateFormat("yyyy-MM");
                calendarType = Calendar.MONTH;
                break;
            case 4:
                sdf = new SimpleDateFormat("yyyy");
                calendarType = Calendar.YEAR;
                break;
            default:
                return null;
        }

        List<String> result = new ArrayList<>();
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        try {
            min.setTime(sdf.parse(startDate));
            min.add(calendarType, 0);
            max.setTime(sdf.parse(endDate));
            max.add(calendarType, 0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar curr = min;
        while (curr.before(max)) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            result.add(formatter.format(min.getTime()));
            curr.add(calendarType, 1);
        }
        return result;
    }


    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 根据用户传入的时间表示格式，返回时间的格式 如果是yyyyMMdd，注意字母y不能大写。
     *
     * @param format yyyyMMddhhmmss
     * @return
     */
    public static String getDateToFormat(Date date,String format) {
        if (format.contains("年") || format.contains("月") || format.contains("日")
                || format.contains("时") || format.contains("分") || format.contains("秒"))
            format = format.replace("年","yyyy").replace("月","MM").replace("日","dd")
                    .replace("时","HH").replace("分","mm").replace("秒","ss");

//        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.format(date);
        }catch (Exception e){
            throw new RRException("时间格式错误，请重新配置！");
        }
    }


    public static boolean isDate(String dttm, String format) {
        if (dttm == null || dttm.isEmpty() || format == null || format.isEmpty()) {
            return false;
        }

        if (format.contains("月") || format.contains("年") || format.contains("日")
                || format.contains("时") || format.contains("分") || format.contains("秒"))
            format = format.replace("年","yyyy").replace("月","MM").replace("日","dd")
                    .replace("时","HH").replace("分","mm").replace("秒","ss");

        DateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(dttm, pos);

        if (date == null || pos.getErrorIndex() > 0) {
            return false;
        }

//        if (pos.getIndex() != dttm.length()) {
//            return false;
//        }

        return true;
    }

    public static void main(String[] args) {
        String date = getDateToFormat(new Date(), "f");
        System.out.println(date);

        System.out.println(isDate("2021-04-25 06:30:00","yyy-MMM"));

    }
}
